package com.stone.weblog.common.response;

import com.stone.weblog.common.exception.BaseExceptionInterface;
import com.stone.weblog.common.exception.BizException;
import lombok.AllArgsConstructor;
import lombok.Data;

import java.io.Serializable;

/**
 * @program: weblog-springboot
 * @author: stone
 * @url: 1755545704@qq.com
 * @create: 2024-01-14 13:09
 * @description:
 */
@Data
public class Response<T> implements Serializable {
    private String message;
    private String code;
    private T data;

    public static <T>Response<T> success(){
        Response<T> response =new Response<>();
        response.setMessage("ok");
        response.setCode("200");
        return  response;
    }
    public static <T> Response<T> success(T data) {
        Response<T> response = new Response<>();
        response.setMessage("ok");
        response.setCode("200");
        response.setData(data);
        return response;
    }

    // =================================== 失败响应 ===================================
    public static <T> Response<T> fail() {
        Response<T> response = new Response<>();
        response.setMessage("fail");
        response.setCode("400");
        return response;
    }

    public static <T> Response<T> fail(String errorMessage) {
        Response<T> response = new Response<>();
        response.setMessage(errorMessage);
        return response;
    }

    public static <T> Response<T> fail(String errorCode, String errorMessage) {
        Response<T> response = new Response<>();
        response.setCode(errorCode);
        response.setMessage(errorMessage);
        return response;
    }

    public static <T> Response<T> fail(BizException bizException) {
        Response<T> response = new Response<>();
        response.setCode(bizException.getErrorCode());
        response.setMessage(bizException.getErrorMessage());
        return response;
    }
    public static <T> Response<T> fail(BaseExceptionInterface baseExceptionInterface) {
        Response<T> response = new Response<>();
        response.setMessage(baseExceptionInterface.getErrorMessage());
        response.setCode(baseExceptionInterface.getErrorCode());
        return response;
    }
}